import 'package:inmotion_pro/pages/models/common_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

// 设置页面
class BorderTableView extends StatelessWidget {
  // 数据源
  final RxList<CommonModel> items;

  // 点击事件
  final Function(int index)? onTap;

  const BorderTableView({
    super.key,
    required this.items,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(
        left: CONTENT_MARGIN,
        right: CONTENT_MARGIN,
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(12.w),
        child: Container(
          color: GrayColor.gray2,
          child: Obx(
            () => ListView.builder(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              // Remove default padding
              padding: EdgeInsets.zero,
              itemCount: items.length,
              itemBuilder: (context, index) {
                var item = items[index];
                return Material(
                  child: Column(
                    children: [
                      ListTile(
                        splashColor: LxCol.backgroundColorContainerActive,
                        onTap: () {
                          if (onTap != null) {
                            onTap!(index);
                          }
                        },
                        title: IntrinsicWidth(
                          child: Row(
                            children: [
                              LxText(
                                item.title,
                                fontWeightEnum: LxFontWeightEnum.bodyMedium,
                              ),
                              SizedBox(width: CONTENT_MARGIN * 2),
                              Expanded(
                                // 填充剩余空间
                                child: Align(
                                  alignment: Alignment.centerRight,
                                  child: LxText(
                                    item.value +
                                        (item.unit.isEmpty
                                            ? ""
                                            : " ${item.unit}"),
                                    fontWeightEnum: LxFontWeightEnum.bodyMedium,
                                    color: Colors.white.withOpacity(0.6),
                                    maxLines: 1,
                                  ),
                                ),
                              ),
                              Visibility(
                                visible: item.imgPath.isNotEmpty,
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  children: [
                                    SizedBox(width: 5.w),
                                    const Icon(
                                      Icons.arrow_forward_ios,
                                      size: 16,
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      Visibility(
                        visible: index != items.length - 1,
                        child: Padding(
                          padding: EdgeInsets.only(
                              left: CONTENT_MARGIN, right: CONTENT_MARGIN),
                          child: Divider(
                            color: GrayColor.gray4.withOpacity(0.3),
                            height: 0.5,
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
